package com.yanqu.road.server.cmd.crossunion;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.union.RechargeUserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.union.UnionRechargeUserProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = CrossProtocol.C_CROSS_UNION_RECHARGE_ACTIVITY_DATA, desc = "")
public class CrossUnionRechargeDataCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUnionActivityRankDataProto.CrossUnionRechargeDataReqMsg reqMsg = CrossUnionActivityRankDataProto.CrossUnionRechargeDataReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        String unionId = reqMsg.getUnionId();
        int msgType = reqMsg.getMsgType();
        if (msgType == 1) {
            UnionRechargeUserProto.UnionRechargeUserSyncMsg.Builder respMsg = UnionRechargeUserProto.UnionRechargeUserSyncMsg.newBuilder();
            UnionActivityConditionData condition = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, 0, unionId);
            if (condition != null) {
                respMsg = parseUnionRechargeUserSyncMsg(activityId, condition);
            }
            YanQuMessage YanQuMessage = YanQuMessageUtils.buildMessage(Protocol.U_UNION_ACTIVITY_RECHARGE_USER_SYNC, respMsg);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessage);
        } else if (msgType == 2) {
            UnionRechargeUserProto.UnionRechargeUserRespMsg.Builder respMsg = UnionRechargeUserProto.UnionRechargeUserRespMsg.newBuilder();
            UnionActivityConditionData condition = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, 0, unionId);
            if (condition != null) {
                respMsg = parseUnionRechargeUserMsg(activityId, condition);
                respMsg.setRet(0);
                YanQuMessage YanQuMessage = YanQuMessageUtils.buildMessage(Protocol.U_UNION_ACTIVITY_RECHARGE_USER, respMsg);
                MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessage);
            }
        }
        UnionActivityConditionData unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, 0, unionId);
        if (unionActivityConditionData != null) {
            CrossUnionActivityRankDataProto.CrossUnionRechargeSyncMsg.Builder syncMsg = CrossUnionActivityRankDataProto.CrossUnionRechargeSyncMsg.newBuilder();
            syncMsg.setActivityId(activityId);
            syncMsg.setUnionId(unionId);
            syncMsg.setInfo(unionActivityConditionData.getInfo());
            YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_SYNC_CROSS_UNION_RECHARGE_INFO, syncMsg);
            MessageHelper.sendPacket(serverId, 0, message);
        }
    }

    public static UnionRechargeUserProto.UnionRechargeUserRespMsg.Builder parseUnionRechargeUserMsg(int activityId, UnionActivityConditionData data) {
        UnionRechargeUserProto.UnionRechargeUserRespMsg.Builder respMsg = UnionRechargeUserProto.UnionRechargeUserRespMsg.newBuilder();
        Map<Integer, List<RechargeUserInfo>> infoMap = JSON.parseObject(data.getInfo(), new TypeReference<Map<Integer, List<RechargeUserInfo>>>() {
        });
        if (infoMap == null) infoMap = new ConcurrentHashMap<>();
        respMsg.setActivityId(activityId);
        for (Integer conditionId : infoMap.keySet()) {
            UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.Builder conditionMsg = UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.newBuilder();
            conditionMsg.setConditionId(conditionId);
            for (RechargeUserInfo info : infoMap.get(conditionId)) {
                UnionRechargeUserProto.UnionRechargeUserTempMsg.Builder userMsg = UnionRechargeUserProto.UnionRechargeUserTempMsg.newBuilder();
                userMsg.setUserId(info.getUserId());
                userMsg.setNickName(CrossUserMgr.getUserName(info.getUserId()));
                userMsg.setTotalRecharge(info.getTotalRecharge());
                userMsg.setIsGetReward(info.isGetReward());
                conditionMsg.addUserList(userMsg);
            }
            respMsg.addConditionList(conditionMsg);
        }
        return respMsg;
    }

    public static UnionRechargeUserProto.UnionRechargeUserSyncMsg.Builder parseUnionRechargeUserSyncMsg(int activityId, UnionActivityConditionData data) {
        UnionRechargeUserProto.UnionRechargeUserSyncMsg.Builder respMsg = UnionRechargeUserProto.UnionRechargeUserSyncMsg.newBuilder();
        Map<Integer, List<RechargeUserInfo>> infoMap = JSON.parseObject(data.getInfo(), new TypeReference<Map<Integer, List<RechargeUserInfo>>>() {
        });
        if (infoMap == null) infoMap = new ConcurrentHashMap<>();
        respMsg.setActivityId(activityId);
        for (Integer conditionId : infoMap.keySet()) {
            UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.Builder conditionMsg = UnionRechargeUserProto.UnionRechargeUserConditionTempMsg.newBuilder();
            conditionMsg.setConditionId(conditionId);
            for (RechargeUserInfo info : infoMap.get(conditionId)) {
                UnionRechargeUserProto.UnionRechargeUserTempMsg.Builder userMsg = UnionRechargeUserProto.UnionRechargeUserTempMsg.newBuilder();
                userMsg.setUserId(info.getUserId());
                userMsg.setNickName(CrossUserMgr.getUserName(info.getUserId()));
                userMsg.setTotalRecharge(info.getTotalRecharge());
                userMsg.setIsGetReward(info.isGetReward());
                conditionMsg.addUserList(userMsg);
            }
            respMsg.addConditionList(conditionMsg);
        }
        return respMsg;
    }

    @Override
    public void sendData() {

    }
}
